﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using conan.Data;
using conan.Domain.Entities;

namespace conan.Repository
{
    public class VisitDomainRep : RepositoryBase<VisitDomain, long>//, IVisitDomainRep
    {
        //public VisitDomainRep(IDbContext dbContext) : base(dbContext)
        //{
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="domain"></param>
        /// <returns>true 存在；false 不存在</returns>
        public async Task<bool> ExistsAsync(string domain)
        {
            int count = await base.RecordCountAsync("where `Domain`=@Domain", new { Domain = domain.ToLower() });
            if (count > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override async Task<int> InsertAsync(VisitDomain entity)
        {
            entity.State = 0;
            entity.Domain = entity.Domain.ToLower();
            return await base.InsertAsync(entity);
        }

        /// <summary>
        /// 状态修改 ，会更新缓存
        /// </summary>
        /// <param name="domain"></param>
        /// <param name="state"></param>
        /// <param name="agentId"></param>
        /// <returns></returns>
        public async Task<bool> ChangeStateAsync(long id, string domain, int state, long agentId)
        {
            return await base.UpdateAsync<VisitDomain>(new { Id = id, state = state });// ("update `visitdomains` set `state`=@state where `domain`=@domain", new { state = state, domain = domain });
        }
    }
}
